﻿/*****************************************************************************
Copyright (c) 2011 SmartVault, Inc.

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
******************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;

namespace SmartVault.OAuth.Serializer
{
    internal class Constants
    {
        internal const int IdSize = 22;
        internal const int ClientIdSize = 45;
        internal const int LongSize = 8;
        internal const int AuthSchemeSize = 5;
        internal const int KeyIdSize = 1;
        internal const int VersionSize = 1;
        internal const int PinSize = 6;

        internal const string ExpiredErrorMessage = "Token expires on {0} current time is {1}";

        static internal void CheckExpiredError(DateTimeOffset expiresOn)
        {
            DateTimeOffset now = DateTimeOffset.Now;

            if (expiresOn.UtcDateTime < now.UtcDateTime)
            {
                throw new InvalidOperationException(string.Format(ExpiredErrorMessage, expiresOn.UtcDateTime,
                                                              now.UtcDateTime));
            }
        }

        public static byte[] JoinArrays(byte[] array1, byte[] array2)
        {
            byte[] newArray =
                  new byte[array1.Length + array2.Length];

            Array.Copy(array1, newArray, array1.Length);
            Array.Copy(array2, 0, newArray, array1.Length, array2.Length);
            return newArray;
        }

        internal static string PadClientId(string id, string name)
        {
            if (string.IsNullOrEmpty(id) || id.Length > ClientIdSize)
            {
                throw new ArgumentException(string.Format("The argument '{0}' does not fall within the expected range.", name));
            }
            else
            {
                int diff = ClientIdSize - id.Length;
                if (diff != 0)
                {
                    StringBuilder bld = new StringBuilder(id);
                    bld.Append('*', diff);
                    id = bld.ToString();
                }
            }

            return id;
        }

        internal static string UnpadClientId(string id, string name)
        {
            if (string.IsNullOrEmpty(id) || id.Length > ClientIdSize)
            {
                throw new ArgumentException(string.Format("The argument '{0}' does not fall within the expected range.", name));
            }
            else
            {
                id = id.Trim('*');
            }

            return id;
        }

        internal static void ValidateClientId(string id, string name)
        {
            if (string.IsNullOrEmpty(id) || id.Length != ClientIdSize)
            {
                throw new ArgumentException(string.Format("The argument '{0}' does not fall within the expected range.", name));
            }
        }

        internal static void ValidateId(string id, string name)
        {
            if (string.IsNullOrEmpty(id) || id.Length != IdSize)
            {
                throw new ArgumentException(string.Format("The argument '{0}' does not fall within the expected range.", name));
            }
        }
    }
}
